//
//  MainViewController.m
//  UI09_ContactBookSample
//
//  Created by lizhongren on 15/9/27.
//  Copyright (c) 2015年 lizhongren. All rights reserved.
//

#import "AddViewController.h"
#import "MainViewController.h"
#import "DetailViewController.h"
#import "Student.h"

@interface MainViewController () <UITableViewDelegate, UITableViewDataSource, AddVCDelegate>

@property (nonatomic, retain) UITableView *tableView;

// 用来装Student对象
@property (nonatomic, retain) NSMutableArray *studentArr;


@end

@implementation MainViewController

- (void)dealloc
{
    [_studentArr release];
    [_tableView release];
    [super dealloc];
}

- (void)handleData
{
    // 处理数据的方法
    
    // 读取plist的数据
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
    
    NSMutableArray *tempArr = [NSMutableArray arrayWithContentsOfFile:path];
    
    // 处理数据
    
    // 容器作为属性, 一定要对其初始化再使用
    self.studentArr = [NSMutableArray array];
    
    
    for (NSDictionary *dic in tempArr) {
        
        // 遍历所有的字典
        // 每个字典都对应一个Student对象
        Student *stu = [[Student alloc] init];
        // 利用KVC系统给对象赋值
        [stu setValuesForKeysWithDictionary:dic];
        
        [self.studentArr addObject:stu];
        
        [stu release];
    }
    
    NSLog(@"%@", self.studentArr);
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 处理数据
    [self handleData];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    // 添加按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonAction:)] autorelease];
    
    // 设置编辑按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (void)barButtonAction:(UIBarButtonItem *)barButton
{
    NSLog(@"添加");
    
    AddViewController *addVC = [[AddViewController alloc] init];
    
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];
}

- (void)addStudent:(Student *)stu
{
    [self.studentArr addObject:stu];
    
    // 刷新tableView的数据
    [self.tableView reloadData];
}

// tableView编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.studentArr removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        [tableView reloadData];
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.studentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    Student *stu = [self.studentArr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = stu.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 点击cell的时候, push出详细信息页面, 并将相应的student对象赋值
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.stu = [self.studentArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
